package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.DbusinessDao;
import cn.gson.fengboot.model.dao.DphaseDao;
import cn.gson.fengboot.model.dao.DtailDao;
import cn.gson.fengboot.model.dao.DttemplateDao;
import cn.gson.fengboot.model.mapper.DbusinessMapper;
import cn.gson.fengboot.model.mapper.DtailMapper;
import cn.gson.fengboot.model.mapper.DttemplateMapper;
import cn.gson.fengboot.model.mapper.DtypeMapper;
import cn.gson.fengboot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class DtailService {

    @Autowired
    DtailMapper dtailMapper;

    @Autowired
    DttemplateMapper dttemplateMapper;

    @Autowired
    DtypeMapper dtypeMapper;

    @Autowired
    DtailDao dtailDao;

    @Autowired
    DttemplateDao dttemplateDao;

    @Autowired
    DbusinessDao dbusinessDao;

    @Autowired
    DphaseDao dphaseDao;

    public List<Dtail> selectAllTails(Integer busId) {
        return dtailMapper.selectAllTails(busId);
    }

    public List<Dttemplate> selectttemplate() {
        return dttemplateMapper.selectttemplate();

    }


    public List<Dtype> selectalltype(){
        return dtypeMapper.selectalltype();
    }

    public void addbustail(Dtail dtail) {
        if(dtail.getTtemplateId().getTtemplateId()==0){
            dtail.setTtemplateId(null);
        }
        dtailDao.save(dtail);

        Integer busId = dtail.getBusId().getBusId();

        /*推进进度*/
        Integer size = selectAllTails(busId).size();
        System.out.println(size);
        if(size==2){
            System.out.println("商机Id:"+busId);
            Dbusiness dbusiness = dbusinessDao.findById(busId).get();
            Integer  phaseId = dbusiness.getPhase().getPhaseId();
            Dphase dphase1 = dphaseDao.findById(phaseId + 1).get();
            dbusiness.setBusPossibility(dphase1.getPhasePoss());
            dbusiness.setPhase(dphase1);
            dbusinessDao.save(dbusiness);
        }
    }

    public void addttem(Dttemplate dttemplate) {
        dttemplateDao.save(dttemplate);
    }

    public void detttem(Integer ttemplateId) {
        dttemplateDao.delete(dttemplateDao.findById(ttemplateId).get());
    }

    public List<Dttemplate> selectttembytypeId(Integer typeId) {
        return dttemplateMapper.findAllByTypeId(typeId);
    }
}
